/*
 *
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *
 *  contributor license agreements. See the NOTICE file distributed with
 *
 *  this work for additional information regarding copyright ownership.
 *
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *
 *  (the "License"); you may not use this file except in compliance with
 *
 *  the License. You may obtain a copy of the License at
 *
 *
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *
 *
 *  Unless required by applicable law or agreed to in writing, software
 *
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 *  See the License for the specific language governing permissions and
 *
 *  limitations under the License.
 *
 * /
 */
package org.apache.kylin.common.streaming;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.kylin.common.util.Pair;

import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 */
public final class StreamingBatch implements Serializable {

    private final List<StreamingMessage> messages;

    private final Pair<Long, Long> timeRange;
    private transient Map<Integer,String> partitionOffsets;

    public StreamingBatch(List<StreamingMessage> messages, Pair<Long, Long> timeRange) {
        this.messages = messages;
        this.timeRange = timeRange;
    }

    public boolean isNullBatch(){
        return messages == null || messages.size() == 0;
    }

    public List<StreamingMessage> getMessages() {
        return messages;
    }

    public Pair<Long, Long> getTimeRange() {
        return timeRange;
    }

    public int size(){
        return this.messages.size();
    }

    private Map<Integer,String> initPartitionOffsets(){
        Map<Integer,String> offsets = Maps.newHashMap();
        if (messages != null){
            Map<Integer,List<StreamingMessage>> partitionMsgs = Maps.newHashMap();
            Iterator<StreamingMessage> messageIterator = messages.iterator();
            while (messageIterator.hasNext()){
                StreamingMessage message = messageIterator.next();
                int partition = message.getPartition();
                if (!partitionMsgs.containsKey(partition)){
                    partitionMsgs.put(partition, Lists.<StreamingMessage>newArrayList());
                }
                partitionMsgs.get(partition).add(message);
            }
            offsets = Maps.transformEntries(partitionMsgs, new Maps.EntryTransformer<Integer, List<StreamingMessage>, String>() {
                @Override
                public String transformEntry(@Nullable Integer integer, @Nullable List<StreamingMessage> messages) {
                    String offsetRange = messages.get(0).getOffset() + "_" + messages.get(messages.size() - 1).getOffset();
                    return offsetRange;
                }
            });
        }
        return offsets;
    }

    public Map<Integer, String> getPartitionOffsets() {
        if (partitionOffsets == null){
            partitionOffsets = initPartitionOffsets();
        }
        return partitionOffsets;
    }

    @Override
    public String toString() {
        return "StreamingBatch{" +
                "messages=" + messages +
                ", timeRange=" + timeRange +
                ", partitionOffsets=" + partitionOffsets +
                '}';
    }
}
